package demo4;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 沈明月
 * Date: 2023-01-03
 * Time: 20:20
 */


//Cloneable 接口和深拷贝

//Cloneable 接口
/*class Student implements Cloneable{
    public String name;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();//父类的克隆
        //protected修饰的
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student student = new Student();
        student.name = "小名";
        Student student1 = (Student)student.clone();//向下转型（强制类型转换）
        System.out.println(student);
        System.out.println(student1);
    }
}*/



//深、浅拷贝
//浅拷贝：
/*class Money{
    public double money = 12.25;
}

class Student implements Cloneable{
    public String name;
    public Money m = new Money();

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student student1 = new Student();

        Student student2 = (Student)student1.clone();//向下转型（强制类型转换）
        System.out.println(student1.m.money);//12.25
        System.out.println(student2.m.money);//12.25

        System.out.println("=========");

        student1.m.money = 19.21;
        System.out.println(student1.m.money);//19.21
        System.out.println(student2.m.money);//19.21
        //这种拷贝就叫做浅拷贝：修改了其中一个，被拷贝的那个也随之发生了改变

        //如果改变了其中的一个，被拷贝的那个没有发生改变，那么这种拷贝就叫做深拷贝
    }
}*/



//深拷贝
/*class Money implements Cloneable{
    public double money = 12.25;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Student implements Cloneable{
    public String name;
    public Money m = new Money();

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Student student = (Student)super.clone();//只是克隆了Student对象
        student.m = (Money)this.m.clone();//克隆了student对象的Money对象
        return student;
        //return super.clone();
    }
}
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student student1 = new Student();

        Student student2 = (Student)student1.clone();//向下转型（强制类型转换）
        System.out.println(student1.m.money);//12.25
        System.out.println(student2.m.money);//12.25

        System.out.println("=========");

        student1.m.money = 19.21;//深拷贝
        System.out.println(student1.m.money);//19.21
        System.out.println(student2.m.money);//12.25
    }
}*/




//Object类：
//Java默认提供的一个类，Java中除了Object类，所有的类都是存在继承关系的。
//默认会继承Object父类，即：所有类的对象都可以使用Object的引用进行接收（Object类是所有类的父类）



